
/**
 * @author: Argenis Leon. argenisleon@gmail.com
 *
 */
if (!mDatePicker) {
    var mDatePicker = {};
}

mDatePicker = function(){
    var _div = null;
    var _date = null;
    
    var currentTime = null;
    var actualYear = "";
    var actualMonth = "";
    var actualDay = "";
    var actualDate = "";
    
    var numMonths = 12;
    
    // Update the day, month and year with the actual Year
    currentDate();
    
    //inject CSS
    var css = '<LINK href="mDatePicker.css" rel="stylesheet" type="text/css">';
    document.getElementsByTagName('head')[0].innerHTML += css;
    
    
    /**
     *
     * @param {Object} div: the div id
     */
    function create(div){
        _div = div;
        currentDate();
        document.getElementById(_div).innerHTML = '<div id="date" name="date" onclick="mDatePicker.printYears();">' + actualDate + '</div>';
    }
    function getDate(){
    
        if (actualDate == null) {
            currentDate();
        }
        return actualDate;
    }
    function setDate(iDay, iMonth, iYear){
        document.getElementById('date').innerHTML = LPad(iDay, 2, '0') + "/" + LPad(iMonth, 2, '0') + "/" + LPad(iYear, 2, '0');
        removeElement('yearContainer');
        removeElement('monthContainer');
        removeElement('dayContainer');
        
    }    
    
    function currentDate(){
        currentTime = new Date();
        actualDay = currentTime.getDate();
        actualMonth = currentTime.getMonth() + 1;
        actualYear = currentTime.getFullYear();
        actualDate = LPad(actualDay, 2, '0') + "/" + LPad(actualMonth, 2, '0') + "/" + LPad(actualYear, 2, '0');
    }
    
    function printYears(){
    
        var baseYear = 2000;
        var range = 5;
		
		var year = new Date().getFullYear();
        
        var lowYear = year - range;
        var highYear = year + range;
        
        removeElement('yearContainer');
        removeElement('monthContainer');
        removeElement('dayContainer');
        
        document.getElementById(_div).innerHTML += '<div id="yearContainer" name="yearContainer"><spam>Year:</spam><spam id="year" name="year"></spam></div>';
        
        var concat = "";
        var clase = "";
        for (var i = lowYear; i <= highYear; i++) {
            // Actual Year
            if (i == year) {
                clase = 'class="actual"';
            }
            else {
                clase = "";
            }
            
            if (i < highYear) {
                concat += '<spam "' + clase + '" id="year' + i + '" name="year' + i + '" onclick="mDatePicker.printMonths(' + i + ')">' + (i + "").substr(2) + '</spam>' + ' ';
            }
            else {
                concat += '<spam "' + clase + '"id="year' + i + '" name="year' + i + '" onclick="mDatePicker.printMonths(' + i + ')">' + (i + "").substr(2) + '</spam>';
            }
            
        }
        document.getElementById('year').innerHTML = concat;
    }
    
    function printMonths(iYear){
        removeElement('monthContainer');
        removeElement('dayContainer');
        document.getElementById(_div).innerHTML += '<div id="monthContainer" name="monthContainer"><spam>Month:</spam><spam id="month" name="month"></spam></div>';
        document.getElementById('date').innerHTML = "<strong>" + iYear + "</strong>";
        
        var realMonth = 0;
        
        // Update to the current date
        currentDate();
        
        
        var concat = "";
        // concatenate all the month in a string
        for (var i = 0; i < numMonths; i++) {
        
            if (i == actualMonth-1 && iYear == actualYear) {
                clase = 'class="actual"';
            }
            else {
                clase = "";
            }
            
            if (i < numMonths) {
                concat += '<spam "' + clase + '"id="month' + i + '" name="month' + i + '" onclick="mDatePicker.printDays(' + i + ',' + iYear + ')">' + LPad(i + 1, 2, '0') + '</spam>' + ' ';
            }
            else {
                concat += '<spam "' + clase + '"id="month' + i + '" name="month' + i + '" onclick="mDatePicker.printDays(' + i + ',' + iYear + ')">' + LPad(i + 1, 2, '0') + '</spam>';
            }
        }
        
        document.getElementById('month').innerHTML = concat;
    }
    
    function printDays(iMonth, iYear){
        removeElement('dayContainer');
        document.getElementById('control').innerHTML += '<div id="dayContainer" name="dayContainer"><spam>Day:</spam><spam id="day" name="day"></spam></div>';
        
        //Put it here we ensure that we made a aritmetic operation. 
        
        document.getElementById('date').innerHTML = "<strong>" + LPad(iMonth + 1, 2, '0') + "/" + iYear + "</strong>"
        document.getElementById('day').innerHTML = "";
        
        // Calculate de days in a month
        var numDays = daysInMonth(iMonth, iYear);
        
        // Update to the current date
        currentDate();
        
        var concat = "";
		//alert(actualDay +"/" + iMonth +"=="+actualMonth);
        for (var i = 1; i <= numDays; i++) {
            if (i == actualDay && iMonth == actualMonth-1 && iYear == actualYear) {
                clase = 'class="actual"';
            }
            else {
                clase = "";
            }
            if (i < numDays) {
                concat += '<spam "' + clase + '"onclick="mDatePicker.setDate(' + i + ',' + (iMonth + 1) + ',' + iYear + ')">' + LPad(i, 2, '0') + '</spam>' + ' ';
            }
            else {
                concat += '<spam "' + clase + '"onclick="mDatePicker.setDate(' + i + ',' + (iMonth + 1) + ',' + iYear + ')">' + LPad(i, 2, '0') + '</spam>';
            }
        }
        document.getElementById('day').innerHTML = concat;
    }
    
    
    // http://snippets.dzone.com/posts/show/2099
    function daysInMonth(iMonth, iYear){
        return 32 - new Date(iYear, iMonth, 32).getDate();
    }
    
    function LPad(ContentToSize, PadLength, PadChar){
    
        var PaddedString = ContentToSize.toString();
        for (var i = PaddedString.length + 1; i <= PadLength; i++) {
            PaddedString = PadChar + PaddedString;
        }
        return PaddedString;
    }
	
	function removeElement(divNum){
        var d = document.getElementById(_div);
        var olddiv = document.getElementById(divNum);
        if (olddiv != null) {
            d.removeChild(olddiv);
        }
    }
    
    return {
        create: create,
        printYears: printYears,
        printMonths: printMonths,
        printDays: printDays,
        setDate: setDate,
        getDate: getDate
    
    }
}();
